package com.ruoyi.project.system.Information.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.common.RequestDemo;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.poi.hpsf.Decimal;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.Information.domain.CustomerInformation;
import com.ruoyi.project.system.Information.service.ICustomerInformationService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.validation.Valid;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-05-10
 */
@Controller
@RequestMapping("/system/Information")
public class CustomerInformationController extends BaseController
{
    private String prefix = "system/Information";

    @Autowired
    private ICustomerInformationService customerInformationService;

    @Autowired
    private IUserService userService;

//    @RequiresPermissions("system:Information:view")
    @GetMapping()
    public String Information(Model model)
    {
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        User userByUserName = userService.selectByUserName(loginUser);
        BigDecimal amount = userByUserName.getAmount();
        BigDecimal spend = userByUserName.getSpend();

        model.addAttribute("loginUser",loginUser);
        model.addAttribute("amount",amount);
        model.addAttribute("spend",spend);
        return prefix + "/Information";
    }

    /**
     * 查询【请填写功能名称】列表
     */
//    @RequiresPermissions("system:Information:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CustomerInformation customerInformation)
    {

        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        startPage();
        if ("admin".equals(loginUser)){
            List<CustomerInformation> list = customerInformationService.selectCustomerInformationList(customerInformation);
            return getDataTable(list);
        }else {
            customerInformation.setLoginName(loginUser);
            List<CustomerInformation> list = customerInformationService.selectCustomerInformationList(customerInformation);
            return getDataTable(list);
        }
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @RequiresPermissions("system:Information:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CustomerInformation customerInformation)
    {
        List<CustomerInformation> list = customerInformationService.selectCustomerInformationList(customerInformation);
        ExcelUtil<CustomerInformation> util = new ExcelUtil<CustomerInformation>(CustomerInformation.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
//    @RequiresPermissions("system:Information:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(@Valid CustomerInformation customerInformation)
    {
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();

        customerInformation.setLoginName(loginUser);
        User userByUserName = userService.selectByUserName(loginUser);

        //上级用户名称
        String createBy = userByUserName.getCreateBy();
        //账号单价
        BigDecimal spend = userByUserName.getSpend();
        //余额
        BigDecimal subtract = userByUserName.getAmount().subtract(userByUserName.getSpend());
        if (subtract.compareTo(BigDecimal.ZERO) < 0){
            throw new ServiceException("余额不足请联系管理员充值!");
        }else {
            if (!"admin".equals(createBy)){
                //上级账号信息
                User user = userService.selectByUserName(createBy);
                //上级账号的上级账号名称
                String createBy1 = user.getCreateBy();
                //上级账号余额
                BigDecimal amount = user.getAmount();
                //上级账号单价
                BigDecimal superiorSpend = user.getSpend();
                //两个级别账号的单价差价
                BigDecimal spread = spend.subtract(superiorSpend);
                BigDecimal newSuperiorAmount = amount.add(spread);
                userService.updateAmount(newSuperiorAmount,createBy);
                if (!"admin".equals(createBy1)){
                    //上级账号的上级账号的账号信息
                    User user1 = userService.selectByUserName(createBy1);
                    //上级账号的上级账号的余额
                    BigDecimal amount1 = user1.getAmount();
                    //上级账号的上级账号的单价
                    BigDecimal spend1 = user1.getSpend();
                    //上上级与上级两个级别账号的单价差价
                    BigDecimal spread1 = superiorSpend.subtract(spend1);
                    BigDecimal newSuperiorAmount1 = amount1.add(spread1);
                    userService.updateAmount(newSuperiorAmount1,createBy1);
                }
            }
            userService.updateAmount(subtract,loginUser);
            customerInformation.setState("待处理");
            customerInformation.setAmount(spend);
            return toAjax(customerInformationService.insertCustomerInformation(customerInformation));
        }
    }

    /**
     * 修改【请填写功能名称】
     */
//    @RequiresPermissions("system:Information:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CustomerInformation customerInformation = customerInformationService.selectCustomerInformationById(id);
        mmap.put("customerInformation", customerInformation);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
//    @RequiresPermissions("system:Information:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CustomerInformation customerInformation)
    {

        return toAjax(customerInformationService.updateCustomerInformation(customerInformation));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @RequiresPermissions("system:Information:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(customerInformationService.deleteCustomerInformationByIds(ids));
    }


    //调取百度api
    @ResponseBody
    @RequestMapping("/getData")
    public String getData(String text){
        RequestDemo.getAuth();
        String text1 = RequestDemo.getText(text);
        if (text1==null){
            throw new ServiceException("请检查地址解析余额是否充足");
        }
        return text1;
    }

    @ResponseBody
    @RequestMapping("/updateState")
    public AjaxResult updateState(Integer id,String state){
        return toAjax(customerInformationService.updateState(id,state));
    }

    @ResponseBody
    @RequestMapping("/updateSign1")
    public AjaxResult updateSign1(Integer id){
        return toAjax(customerInformationService.updateSign1(id));
    }

    @ResponseBody
    @RequestMapping("/updateSign2")
    public AjaxResult updateSign2(Integer id){
        return toAjax(customerInformationService.updateSign2(id));
    }

    @ResponseBody
    @RequestMapping("/updateSign3")
    public AjaxResult updateSign3(Integer id){
        return toAjax(customerInformationService.updateSign3(id));
    }

    //退款
    @ResponseBody
    @RequestMapping("/amountAdd")
    @Transactional
    public AjaxResult amountAdd(String loginUser,Long id){
        // 获取当前的用户信息
//        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
//        String loginUser = currentUser.getLoginName();
        //根据订单的登录名称查询该账号信息
        customerInformationService.updateFlag(id);
        User userByUserName = userService.selectByUserName(loginUser);
        //获取该账号的单价
        BigDecimal spend = userByUserName.getSpend();
        //获取上级用户名
        String createBy = userByUserName.getCreateBy();
        //获取上级账号信息
        User user = userService.selectByUserName(createBy);
        if (!"admin".equals(user.getLoginName())) {
            //上级账号单价
            BigDecimal superiorSpend = user.getSpend();
            //上级的余额
            BigDecimal amount = user.getAmount();
            //两个级别之间的差价
            BigDecimal spread = spend.subtract(superiorSpend);
            //新的上级余额
            BigDecimal newSuperiorAmount = amount.subtract(spread);
            customerInformationService.amountAdd(newSuperiorAmount,createBy);

            String createBy1 = user.getCreateBy();
            if (!"admin".equals(createBy1)){
                //获取上级账号的上级账号的信息
                User user1 = userService.selectByUserName(createBy1);
                //获取上级账号的上级账号的余额
                BigDecimal amount1 = user1.getAmount();
                //获取上级账号的上级账号的单价
                BigDecimal spend1 = user1.getSpend();
                //上级账号和上级账号和上级账号的单价
                BigDecimal spread1 = superiorSpend.subtract(spend1);
                BigDecimal newSuperiorAmount1 = amount1.subtract(spread1);
                customerInformationService.amountAdd(newSuperiorAmount1,createBy1);
            }

        }
        BigDecimal oldAmount = userByUserName.getAmount();
        BigDecimal newAmount=oldAmount.add(spend);
        return toAjax(customerInformationService.amountAdd(newAmount,loginUser));
    }
    //单条获取数据
    @ResponseBody
    @RequestMapping("/selectSingle")
    public synchronized CustomerInformation selectSingle(){
        CustomerInformation customerInformation = customerInformationService.selectSingle();
        customerInformationService.updateFlag(customerInformation.getId());
        return customerInformation;
    }


    //直接调用的退单接口
    @ResponseBody
    @RequestMapping("/chargeback")
    @Transactional
    public AjaxResult chargeback(String loginName,Integer id){
//        String state="订单退款";
        //根据订单的登录名称查询该账号信息
        User userByUserName = userService.selectByUserName(loginName);
        //获取该账号的单价
        BigDecimal spend = userByUserName.getSpend();
        //获取上级用户名
        String createBy = userByUserName.getCreateBy();
        //获取上级账号信息
        User user = userService.selectByUserName(createBy);
        if (!"admin".equals(user.getLoginName())) {
            //上级账号单价
            BigDecimal superiorSpend = user.getSpend();
            //上级的余额
            BigDecimal amount = user.getAmount();
            //两个级别之间的差价
            BigDecimal spread = spend.subtract(superiorSpend);
            //新的上级余额
            BigDecimal newSuperiorAmount = amount.subtract(spread);
            customerInformationService.amountAdd(newSuperiorAmount,createBy);
            String createBy1 = user.getCreateBy();
            if (!"admin".equals(createBy1)){
                //获取上级账号的上级账号的信息
                User user1 = userService.selectByUserName(createBy1);
                //获取上级账号的上级账号的余额
                BigDecimal amount1 = user1.getAmount();
                //获取上级账号的上级账号的单价
                BigDecimal spend1 = user1.getSpend();
                //上级账号和上级账号和上级账号的单价
                BigDecimal spread1 = superiorSpend.subtract(spend1);
                BigDecimal newSuperiorAmount1 = amount1.subtract(spread1);
                customerInformationService.amountAdd(newSuperiorAmount1,createBy1);
            }
        }
//        customerInformationService.updateState(id,state);
        BigDecimal oldAmount = userByUserName.getAmount();
        BigDecimal newAmount=oldAmount.add(spend);
        return toAjax(customerInformationService.amountAdd(newAmount,loginName));
    }
//
//    @ResponseBody
//    @RequestMapping("/aaa")
//    public Integer aaa(Integer id){
//        String state="订单退款";
//        int i = customerInformationService.updateState(id,state);
//        return i;
//    }
}
